package bma.common.netty.webserver.common;

import bma.common.langutil.ai.stack.AIStack;
import bma.common.netty.webserver.Matcher;
import bma.common.netty.webserver.RequestContext;
import bma.common.netty.webserver.RequestDispatcher;
import bma.common.netty.webserver.RequestProcessor;
import bma.common.netty.webserver.matcher.MatcherCommonPattern;
import bma.common.netty.webserver.matcher.MatcherEquals;
import bma.common.netty.webserver.matcher.MatcherRegex;
import bma.common.netty.webserver.matcher.MatcherStartWith;

public class RequestDispatcherMatch extends RequestDispatcher {

	protected Matcher matcher;
	protected RequestProcessor processor;

	public RequestDispatcherMatch() {
		super();
	}

	public RequestDispatcherMatch(RequestProcessor processor) {
		super();
		this.processor = processor;
	}

	public Matcher getMatcher() {
		return matcher;
	}

	public void setMatcher(Matcher matcher) {
		this.matcher = matcher;
	}

	public void setStartWith(String p) {
		this.matcher = new MatcherStartWith(p);
	}

	public void setSame(String p) {
		this.matcher = new MatcherEquals(p);
	}

	public void setCommonPattern(String p) {
		this.matcher = new MatcherCommonPattern(p);
	}

	public void setRegex(String p) {
		this.matcher = new MatcherRegex(p);
	}

	public RequestProcessor getProcessor() {
		return processor;
	}

	public void setProcessor(RequestProcessor processor) {
		this.processor = processor;
	}

	@Override
	public boolean dispatch(AIStack<Boolean> stack, RequestContext ctx)
			throws Exception {
		if (!this.matcher.match(ctx.getPath())) {
			return false;
		}
		return dispatchOrProcess(stack, processor, ctx);
	}

}
